/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model.xml;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.thoughtworks.xstream.XStream;

import entities.vo.ArticuloEnvtVO;
import entities.vo.ArticuloVO;
import entities.vo.ENVTVO;
import entities.vo.HogarVO;
import entities.vo.OFADVO;
import entities.vo.RopaVO;

/**
 *
 * @author Jhonny
 */
public class ParseXML {



	  public OFADVO getOFAD(String path) {
	        OFADVO ofad = new OFADVO();
	  
			try {
				File file = new File(path);
				DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
				DocumentBuilder db = dbf.newDocumentBuilder();
				Document doc = db.parse(file);
				doc.getDocumentElement().normalize();
				
				NodeList nodesNroOfad = doc.getElementsByTagName("nroOfad");
				Node nodeNroOfad = nodesNroOfad.item(0);
				int nroOfad = Integer.parseInt(nodeNroOfad.getTextContent());
				ofad.setNroOfad(nroOfad);
				
				NodeList nodesIdCasaCentral = doc.getElementsByTagName("idCasaCentral");
				Node nodeIdCasaCentral = nodesIdCasaCentral.item(0);
				int idCasaCentral = Integer.parseInt(nodeIdCasaCentral.getTextContent());
				ofad.setIdCasaCentral(idCasaCentral);
							
				NodeList nodeLst = doc.getElementsByTagName("articulo");
				for (int i = 0; i < nodeLst.getLength(); i++) {
					Node itArticulo = nodeLst.item(i);
					if (itArticulo.getNodeType() == Node.ELEMENT_NODE) {
						Element itElmnt = (Element) itArticulo;
						
						ArticuloVO articulo;
						Node nodoArt = itElmnt.getElementsByTagName("ropa").item(0);
						if (nodoArt != null){ //es ropa
							articulo = new RopaVO();
							Element elmntRopa = (Element) nodoArt;
							((RopaVO)articulo).setOrigen(elmntRopa.getElementsByTagName("origen").item(0).getTextContent());
							((RopaVO)articulo).setTalle(elmntRopa.getElementsByTagName("talle").item(0).getTextContent());
						} else { //es hogar
							nodoArt = itElmnt.getElementsByTagName("hogar").item(0);
							articulo = new HogarVO();
							Element elmntHogar = (Element) nodoArt;
							((HogarVO)articulo).setNombre(elmntHogar.getElementsByTagName("nombre").item(0).getTextContent());
							((HogarVO)articulo).setComposicion(elmntHogar.getElementsByTagName("composicion").item(0).getTextContent());
							((HogarVO)articulo).setCategoria(elmntHogar.getElementsByTagName("categoria").item(0).getTextContent());
							((HogarVO)articulo).setMedidas(elmntHogar.getElementsByTagName("medidas").item(0).getTextContent());					
						}
						articulo.setReferencia(Integer.parseInt(itElmnt.getElementsByTagName("referencia").item(0).getTextContent()));
						articulo.setLinea(itElmnt.getElementsByTagName("linea").item(0).getTextContent());
						articulo.setDescripcion(itElmnt.getElementsByTagName("descripcion").item(0).getTextContent());
						articulo.setColor(itElmnt.getElementsByTagName("color").item(0).getTextContent());
						articulo.setSeccion(itElmnt.getElementsByTagName("seccion").item(0).getTextContent());
						float precioUnitario = Float.parseFloat(itElmnt.getElementsByTagName("precioUnitario").item(0).getTextContent());
						articulo.setPrecioUnitario(precioUnitario);
						float precioOferta = Float.parseFloat(itElmnt.getElementsByTagName("precioOferta").item(0).getTextContent());
						if (precioOferta >= 0){
							articulo.setPrecioOferta(precioOferta);
						} else {
							articulo.setPrecioOferta(precioUnitario);
						}						
						ofad.addArticulo(articulo);
						} else {
							System.out.println("ParseXMLjdom: Not an element node");
						}
					}
			} catch (FileNotFoundException e) {
				System.out.println("Archivo XML no encontrado.");
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SAXException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	       
	        return ofad;
	    }
    
    public ENVTVO getArticulosENVT(String path) throws FileNotFoundException, IOException {
        ENVTVO envt=null;
        XStream xstream =  new XStream();
        String datoXML = obtenerContenido(path);
        //xstream.processAnnotations(ENVTVO.class);
        xstream.alias("envt", ENVTVO.class);
        xstream.addImplicitCollection(ENVTVO.class, "articulos", "articulo", ArticuloEnvtVO.class);
        xstream.aliasAttribute(ArticuloEnvtVO.class, "cantidadRecibida", "cantidad");
        envt = (ENVTVO)xstream.fromXML(datoXML);
        return envt;

    }
    private String obtenerContenido(String path) throws FileNotFoundException, IOException {
        StringBuffer contenido = new StringBuffer();
        File file = new File(path);
        if(file.exists()){
            FileInputStream fileInputStream = new FileInputStream(path);
            DataInputStream input = new DataInputStream(fileInputStream);
            while (input.available() !=0){
                contenido.append(input.readLine());
            }
            input.close();
        }
        return contenido.toString();
    }



}
